package com.chf.job.controller;

import com.chf.job.common.Const;
import com.chf.job.common.ServerResponse;
import com.chf.job.pojo.Company;
import com.chf.job.pojo.ReleaseInfo;
import com.chf.job.pojo.User;
import com.chf.job.service.CompanyService;
import com.chf.job.service.ReleaseService;
import com.chf.job.service.ReleaseVoService;
import com.chf.job.vo.ReleaseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ReleaseController {
    @Autowired
    private ReleaseService releaseService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private ReleaseVoService releaseVoService;
    /**
     * 根据注册邮箱和公司id添加招聘信息
     */

    @PostMapping("release/addRelease.html")
    @ResponseBody
    public ServerResponse addRelease(HttpServletRequest request, ReleaseInfo releaseInfo) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        Company company = companyService.getByEmail(user.getEmail());
        int result = releaseService.addRelease(releaseInfo,company.getId(), user.getEmail());
        if (result > 0) {
            return ServerResponse.createBySuccess("添加成功", Const.ADD_RELEASE_SUCCESS);
           }
        return ServerResponse.createByErrorMessage("添加失败");
    }

    /**
     * 用户查看自己公司以及发布的信息
     */
    @GetMapping("release/getReleaseInfo.html")
    @ResponseBody
    public ServerResponse getReleaseInfo(HttpServletRequest request) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        ReleaseVo releaseVo = releaseVoService.getByEmailAndReleaseId(user.getEmail(), 1);
        return ServerResponse.createBySuccess("查询成功", releaseVo);
    }
    /**
     * 有效职位界面修改职位状态
     */
    @GetMapping("release/updatePositStatus")
    public String updatePositStatus(@RequestParam("releaseId") Integer releaseId, @RequestParam("status") Integer status) {
        int result = releaseService.updatePositStatus(releaseId, status);
        if (result > 0) {
            return "redirect:/page/effectivePosition.html";
        }
        return "";
    }

    /**
     * 无效职位界面修改职位状态
     */
    @GetMapping("release/updateUnPositStatus")
    public String updateUnPositStatus(@RequestParam("releaseId") Integer releaseId, @RequestParam("status") Integer status) {
        int result = releaseService.updatePositStatus(releaseId, status);
        if (result > 0) {
            return "redirect:/page/invalidPosition.html";
        }
        return "";
    }

    /**
     * 搜索职位
     */
    @RequestMapping("release/searchRelease.html")
    public String searchRelease(@RequestParam("searchType") String searchType,
                                @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam("search") String search,
                                @RequestParam("companySize") String companySize,
                                @RequestParam("experience") String experience,
                                @RequestParam("xingZhi") String xingZhi,
                                ModelMap modelMap) {
        if (searchType.equals("职位")) {
            if (org.apache.commons.lang3.StringUtils.isBlank(search)) {
                search = null;
            }
            if (org.apache.commons.lang3.StringUtils.isBlank(xingZhi)) {
                xingZhi = null;
            }
            if (org.apache.commons.lang3.StringUtils.isBlank(experience)) {
                experience = null;
            }
            if (org.apache.commons.lang3.StringUtils.isBlank(companySize)) {
                companySize = null;
            }
            PageInfo<ReleaseVo> releaseVoList = releaseVoService.getBySearchTypeAndPositName(search, companySize, experience, xingZhi, pageNum, pageSize);
            modelMap.addAttribute("releaseVoList", releaseVoList);
            modelMap.addAttribute("search", search);
            return "positList";
        }
        List<Company> companyList =  companyService.getByCompanyName(search);
        modelMap.addAttribute("companyList", companyList);
        return "companyList";
    }
}
